
/*
 *************************************************************************
 *西瑞克斯研发管理系统
 *版 本: 1.0   
 *日 期：02/16/2012    
 *作 者： 王浩 QQ：873987060   
 *************************************************************************   
 */
package com.application.yfprojectplan.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.Session;

import com.application.yfprojectplan.entity.YfProjectPlan;
import com.application.yfprojectplan.struts.form.YfProjectPlanForm;
import com.manager.commons.CommonUtil;
import com.manager.commons.dao.HibernateGenericDao;
import com.manager.commons.page.PageBean;

/**
 * 
 * Description:YfProjectPlanDAO<br />
 * 
 * Version:V0.1<br />
 * 
 * Author: 王浩 QQ：873987060 <br />
 * 
 * History: <br />
 * 
 * 02/16/2012
 * 
 */
public class YfProjectPlanDAO extends HibernateGenericDao<YfProjectPlan> {
	private static final Logger logger = Logger
			.getLogger(YfProjectPlanDAO.class);

	private YfProjectPlan yfProjectPlan;

	public YfProjectPlan getYfProjectPlan() {
		return this.yfProjectPlan;
	}

	public void setYfProjectPlan(YfProjectPlan yfProjectPlan) {
		this.yfProjectPlan = yfProjectPlan;
	}

	public Integer getAllCountYfProjectPlan(YfProjectPlanForm yfProjectPlanForm) {
		List args = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql
				.append(" SELECT COUNT(0) FROM yf_project_plan  t left join yf_project p on t.project_no = p.project_no left join usertable u on p.project_manager = u.id left join department b on p.project_org = b.id WHERE 1=1 ");
		sql = getStringBuffer(yfProjectPlanForm, sql, args);
		return super.getCount(sql.toString(), args.toArray());
	}

	public void deleteYfProjectPlans(YfProjectPlanForm yfProjectPlanForm) {
		String hql = " delete from YfProjectPlan where id = '"+ yfProjectPlanForm.getId() + "'";
		Session session = this.getSession();
		try {
			session.createQuery(hql).executeUpdate();
		} catch (RuntimeException re) {
			logger.error("deleteYfProjectPlans failed");
			throw re;
		} finally {
			if (session != null) {
				releaseSession(session);
			}
		}
	}

	public List searchYfProjectPlan(YfProjectPlanForm yfProjectPlanForm,
			PageBean pageBean) {
		List args = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT t.*,u.rname rname,p.*,b.name project_org_name FROM yf_project_plan  t left join yf_project p on t.project_no = p.project_no left join usertable u on p.project_manager = u.id left join department b on p.project_org = b.id WHERE 1=1 ");
		sql = getStringBuffer(yfProjectPlanForm, sql, args);
		sql.append(" order by t.create_date desc ");
		return getPageInfo(pageBean, sql.toString(), args);
	}
	
	
	public List searchYfProjectPlanMax(YfProjectPlanForm yfProjectPlanForm,PageBean pageBean) {
		List args = new ArrayList();
		StringBuffer sql = new StringBuffer();
		sql.append(" select a.end_date change_cdate   from yf_project_plan_item a left join yf_project_plan b on a.PLAN_ID= b.id  WHERE 1=1 ");
		sql.append(" and b.PROJECT_NO = ?  order by end_date desc ");
		args.add(yfProjectPlanForm.getProjectNo());
		return getPageInfo(pageBean, sql.toString(), args);
	}
	
	
    
	public void deleteYfProjectPlanItems(String planId){
		String hql = " delete from YfProjectPlanItem where planId='" + planId +  "'";
	     Session session = this.getSession();
	try {
		session.createQuery(hql).executeUpdate();
	} catch (RuntimeException re) {
		logger.error("deleteYfProjectPlanItems failed");
		throw re;
	} finally {
		if (session != null) {
			releaseSession(session);
		}
	}
	}
	
	public List getYfProjectPlanItems(String planId){
		try {
			StringBuffer hql = new StringBuffer(" from YfProjectPlanItem where planId=? order by orderNo desc ");
			List args = new ArrayList();
			args.add(planId);
			return queryByHql(hql.toString(), args);
		} catch (Exception e) {
			logger.error("getYfProjectPlanItems failed");
		}
		return null;

	}
	/**
	 * 查询条件的封装
	 * 
	 * @param departmentForm
	 * @return
	 */
	private StringBuffer getStringBuffer(YfProjectPlanForm yfProjectPlanForm,
			StringBuffer buf, List args) {
		/*
		 * 需要加入查询条件时封装
		 */

		if (StringUtils.isNotBlank(yfProjectPlanForm.getProjectNo())) {
			buf.append(" and t.PROJECT_NO like ? ");
			args.add("%" + yfProjectPlanForm.getProjectNo().trim() + "%");
		}

		if (null != yfProjectPlanForm.getAuditStatus()
				&& yfProjectPlanForm.getAuditStatus().longValue() != 0) {
			buf.append(" and t.AUDIT_STATUS = ? ");
			args.add(yfProjectPlanForm.getAuditStatus());
		}
		
		if (StringUtils.isNotBlank(yfProjectPlanForm.getPlanType())) {
			buf.append(" and t.PLAN_TYPE = ? ");
			args.add(yfProjectPlanForm.getPlanType());
		}

		if (StringUtils.isNotBlank(yfProjectPlanForm.getId())) {
			buf.append(" and t.id = ? ");
			args.add(yfProjectPlanForm.getId());
		}
		if (StringUtils.isNotBlank(yfProjectPlanForm.getProjectName())) {
			buf.append(" and p.project_name like ? ");
			args.add("%" + yfProjectPlanForm.getProjectName().trim() + "%");
		}
		if (StringUtils.isNotBlank(yfProjectPlanForm.getProjectOrg())) {
			buf.append(" and p.project_org = ? ");
			args.add(yfProjectPlanForm.getProjectOrg());
		}
		if (null != yfProjectPlanForm.getProjectType()
				&& yfProjectPlanForm.getProjectType().longValue() != 0) {
			buf.append(" and p.project_type = ? ");
			args.add(yfProjectPlanForm.getProjectType());
		}
		if (StringUtils.isBlank(yfProjectPlanForm.getId())) {
			buf = CommonUtil.filterData(buf,args);
		}
		

		return buf;
	}

}
